#ifndef TASK_H
#define TASK_H

#include <QObject>
#include <QDateTime>

#include "types.h"

class Task : public QObject
{
    Q_OBJECT
    Q_PROPERTY(uint64_t id READ getId CONSTANT)
    Q_PROPERTY(bool isEnabled READ isEnabled WRITE setEnabled NOTIFY onIsEnabledChanged)
    Q_PROPERTY(Types::TaskType taskType READ getTaskType WRITE setTaskType NOTIFY onTaskTypeChanged)
    Q_PROPERTY(Types::DrivingCurrent current READ getCurrent WRITE setCurrent NOTIFY onTaskTypeChanged)
    Q_PROPERTY(QDateTime startTime READ getStartTime WRITE setStartTime NOTIFY onStartTimeChanged)
    Q_PROPERTY(bool repeat READ isRepeat WRITE setRepeat NOTIFY onRepeatChanged)
    Q_PROPERTY(quint64 interval READ getInterval WRITE setInterval NOTIFY onIntervalChanged)
    Q_PROPERTY(quint16 freq READ getFreq WRITE setFreq NOTIFY onFreqChanged)
    Q_PROPERTY(quint8 priority READ getPriority WRITE setPriority NOTIFY onPriorityChanged)

public:
    explicit Task(QObject *parent = nullptr)
        : QObject(parent), is_enabled(true), id(reinterpret_cast<uint64_t>(this)), taskType(Types::GET_VOLTAGE), startTime(QDateTime::currentDateTime()), repeat(true), interval(5000), freq(0) {
          };
    virtual ~Task() {}
    quint8 priority = 0; // 0: low

signals:
    void onIsEnabledChanged(bool enabled);
    void onTaskTypeChanged(Types::TaskType type);
    void onStartTimeChanged(const QDateTime &time);
    void onRepeatChanged(bool repeat);
    void onIntervalChanged(quint64 interval);
    void onFreqChanged(quint16 freq);
    void onPriorityChanged(quint8 priority);

public:
    // bool isEnabled() const { return is_enabled; }
    bool isEnabled() const { return is_enabled; }
    uint64_t getId() const { return id; }
    Types::TaskType getTaskType() const { return taskType; }
    Types::DrivingCurrent getCurrent() const { return current; }
    QDateTime getStartTime() const { return startTime; }
    bool isRepeat() const { return repeat; }
    quint64 getInterval() const { return interval; }
    quint16 getFreq() const { return freq; }
    quint8 getPriority() const { return priority; }
          
    void setEnabled(bool enabled)
    {
        if (is_enabled != enabled)
        {
            is_enabled = enabled;
            emit onIsEnabledChanged(enabled);
        }
    }
    void setTaskType(Types::TaskType type)
    {
        if (taskType != type)
        {
            taskType = type;
            emit onTaskTypeChanged(type);
        }
    }

    void setCurrent(Types::DrivingCurrent c)
    {
        if (current != c)
        {
            current = c;
            emit onTaskTypeChanged(taskType); // Emit task type change to update current
        }
    }

    void setStartTime(const QDateTime &time)
    {
        if (startTime != time)
        {
            startTime = time;
            emit onStartTimeChanged(time);
        }
    }

    void setRepeat(bool r)
    {
        if (repeat != r)
        {
            repeat = r;
            emit onRepeatChanged(r);
        }
    }

    void setInterval(quint64 i)
    {
        if (interval != i)
        {
            interval = i;
            emit onIntervalChanged(i);
        }
    }

    void setFreq(quint16 f)
    {
        if (freq != f)
        {
            freq = f;
            emit onFreqChanged(f);
        }
    }

    void setPriority(quint8 p)
    {
        if (priority != p)
        {
            priority = p;
            emit onPriorityChanged(p);
        }
    }

private:
    bool is_enabled;
    uint64_t id;
    Types::TaskType taskType;
    Types::DrivingCurrent current;
    QDateTime startTime;
    bool repeat = false;
    quint64 interval; // ms
    quint16 freq;
};

#endif // TASK_H
